package com.mustafaiev.tair.cts.tapestry.encoder;

import org.apache.tapestry5.ValueEncoder;

import com.mustafaiev.tair.cts.facade.IPurchaseFacade;
import com.mustafaiev.tair.cts.tapestry.dto.PurchaseDTO;

public class PurchaseEncoder implements ValueEncoder<PurchaseDTO> {

    private final IPurchaseFacade facade;

    public PurchaseEncoder(final IPurchaseFacade facade) {
        this.facade = facade;
    }

    public String toClient(final PurchaseDTO value) {
        String id = "";
        if(value != null){
            id = String.valueOf(value.getId());
        }
        return id;
    }

    public PurchaseDTO toValue(final String clientValue) {
        PurchaseDTO purchaseDTO = null;
        if((clientValue == null) || "null".equals(clientValue)){
            purchaseDTO = new PurchaseDTO();
        } else {
            purchaseDTO = facade.retrievePurchase(Long.valueOf(clientValue));
        }

        return purchaseDTO;
    }

}
